		******************************************
		** Do Group-Based Inequalities Feel More Unjust?**
		** Leipziger, Aarslew, & Christensen	**
		** 										**
		** Data cleaning code					**
		******************************************
		****Requires the "revrs" package to run****
		******************************************
		
** Setting working directory in master file: 
clear all
		
**# Cleaning the India data
*** Clean IN data ***
use "data/IN_survey.dta",clear 

** Attenion screeners: 
gen attention_1 = 1 
replace attention_1 = 0 if qScreen1 == 1

gen attention_2 = .
replace attention_2 = 0 if qScreen2_3 == 1 & qScreen2_5 == 1  		// respondents who selected both green AND red (as instructed)
replace attention_2 = 1 if qScreen2_1 != 0 | qScreen2_2 != 0 | ///
	qScreen2_4 != 0 | qScreen2_6 != 0 | qScreen2_7 != 0 | ///
	qScreen2_8 !=0 | qScreen2_9 != 0 

gen screenouts = 1 
replace screenouts = 0 if attention_1 == 0 & attention_2 == 0 		// Final sample = 1.614  respondents who passed both attention screeners. 	

drop if screenouts == 1												// dropping screenouts

***Factual manipulation check
gen factual_mani=0
replace factual_mani =1 if q14_E1==2 | q14_E2==1
replace factual_mani=. if BlockE==3 


** Treatment variable: 
recode BlockE (3=0 "Control group") ///
	(1=2 "Intergroup ineq") ///
	(2=1 "Interpersonal ineq") ///
	, gen(D)

** Outcome variables: 
* Perceived fairness: 
gen fair = q15 
replace fair=. if q15==8
sum fair,de
replace fair = ((fair -`r(min)')/(`r(max)'-`r(min)'))*1				
lab def fairness 0 "Very unfair" 1 "Very fair" 
lab val fair fairness

egen fair_std = std(q15) if q15!=8  // standardized with a mean of 0 and a standard deviation of 1

* Perceived effort: 
gen effort = q16
replace effort=. if q16==77
sum effort,de
replace effort = ((effort -`r(min)')/(`r(max)'-`r(min)'))*1		// same
lab def effort 0 "Only luck" 1 "Only effort" 
lab val effort effort

egen effort_std = std(q16) if q16!=77 // standardized with a mean of 0 and a standard deviation of 1


* Grievances (anger): 
gen angry = q17_A
sum angry,de
replace angry = ((angry -`r(min)')/(`r(max)'-`r(min)'))*1
lab def angry 0 "V. low anger" 1 "V. high anger"
lab val angry angry

egen angry_std = std(q17_A)  // standardized with a mean of 0 and a standard deviation of 1


* Grievances (frustration): 
gen frustrated = q17_B
sum frustrated,de
replace frustrated = ((frustrated -`r(min)')/(`r(max)'-`r(min)'))*1
lab def frustrated 0 "V. low frustration" 1 "V. high frustration"
lab val frustrated frustrated

egen frustrated_std = std(q17_B)  // standardized with a mean of 0 and a standard deviation of 1
 

* Hopefulness: 
gen hopeful = q17_C
sum hopeful,de
replace hopeful = ((hopeful -`r(min)')/(`r(max)'-`r(min)'))*1
lab def hopeful 0 "V. little hope" 1 "V. high hope"
lab val hopeful hopeful

** Quasi pre-treatment outcome variables: 
* Fairness
gen pretreat_fair = q1
replace pretreat_fair=. if q1==6
replace pretreat_fair = (pretreat_fair - 1)/4
lab val pretreat_fair fairness
lab var pretreat_fair "Pretreatment fairness"

* Effort
gen pretreat_effort = q2
replace pretreat_effort=. if q2==6
replace pretreat_effort = (pretreat_effort-1)/4
label def effort_pre 0 "Only chance" 1 "Only effort" 
label val pretreat_effort effort_pre

lab var pretreat_effort "Pretreatment effort"

** Covariates (pre-treatment)
* Race centrality scale
revrs q3_1,replace			
revrs q3_6,replace
factor q3_1 q3_2 q3_3 q3_4 q3_5 q3_6,pcf //generating two RC-scales because q3_1 & q3_6 reflects another dimension than the others
alpha q3_2 q3_3 q3_4 q3_5, casewise gen(RC_1) min(1) 
alpha q3_1 q3_6, casewise gen(RC_2) min(1)

* Social dominance orientation (higher values = higher SDO)
revrs q5_3,replace
revrs q5_4,replace
revrs q5_7,replace
revrs q5_8,replace

factor q5_1 q5_2 q5_3 q5_4 q5_5 q5_6 q5_7 q5_8,pcf

alpha q5_1 q5_2 q5_5 q5_6 , casewise gen(SDO_1) min(1)
alpha q5_3 q5_4 q5_7 q5_8 , casewise gen(SDO_2) min(1)

* Compassion
revrs q6_2,replace
revrs q6_3,replace
revrs q6_5,replace
factor q6_1 q6_2 q6_3 q6_4 q6_5,pcf
alpha q6_1 q6_4 , casewise gen(COM_1) min(1)
alpha q6_2 q6_3 q6_5, casewise gen(COM_2) min(1)

* Ideology
gen lib_con = q7
replace lib_con =. if q7 ==8 | q7==9

* Party ID
gen party=q8
replace party=. if q8==6 | q8==7

**household income
tab gross_household_in

gen hh_income_old= gross_household_in
replace gross_household_in = . if hh_income_old>12
replace gross_household_in = 8 if hh_income_old>=8 & gross_household_in!=.
rename gross_household_in hh_income

*Country
gen country=1

save "data/IN_data_clean.dta", replace


**# Cleaning the South Africa data
use "data/ZA_survey.dta",clear 

** Attenion screeners: 
gen attention_1 = 1 
replace attention_1 = 0 if qScreen1 == 1

gen attention_2 = .
replace attention_2 = 0 if qScreen2_3 == 1 & qScreen2_5 == 1  		// respondents who selected both green AND red (as instructed)
replace attention_2 = 1 if qScreen2_1 != 0 | qScreen2_2 != 0 | 		///
	qScreen2_4 != 0 | qScreen2_6 != 0 | qScreen2_7 != 0 | 			///
	qScreen2_8 !=0 | qScreen2_9 != 0 

gen screenouts = 1 
replace screenouts = 0 if attention_1 == 0 & attention_2 == 0 		// Final sample = 1.625  respondents who passed both attention screeners. 	

drop if screenouts == 1			// dropping screenouts

***Factual manipulation check
gen factual_mani=0
replace factual_mani =1 if q14_E1==2 | q14_E2==1
replace factual_mani=. if BlockE==3 

** Treatment variable: 
recode BlockE (3=0 "Control group") ///
	(1=2 "Intergroup ineq") ///
	(2=1 "Interpersonal ineq") ///
	, gen(D)

** Outcome variables: 
* Perceived fairness: 
gen fair = q15 
replace fair=. if q15==8
sum fair,de		
replace fair = ((fair -`r(min)')/(`r(max)'-`r(min)'))*1		
lab def fairness 0 "Very unfair" 1 "Very fair" 
lab val fair fairness
	
egen fair_std = std(q15) if q15!=8  // standardized with a mean of 0 and a standard deviation of 1

	
* Perceived effort: 
gen effort = q16
replace effort=. if q16==77
sum effort,de
replace effort = ((effort -`r(min)')/(`r(max)'-`r(min)'))*1		// same
lab def effort 0 "Only luck" 1 "Only effort" 
lab val effort effort

egen effort_std = std(q16) if q16!=77  // standardized with a mean of 0 and a standard deviation of 1


* Grievances (anger): 
gen angry = q17_A
replace angry = angry/10
lab def angry 0 "V. low anger" 1 "V. high anger"
lab val angry angry

egen angry_std = std(q17_A)  // standardized with a mean of 0 and a standard deviation of 1


* Grievances (frustration): 
gen frustrated = q17_B
replace frustrated = frustrated/10
*replace frustrated = ((frustrated -`r(min)')/(`r(max)'-`r(min)'))*1
lab def frustrated 0 "V. low frustration" 1 "V. high frustration"
lab val frustrated frustrated

egen frustrated_std = std(q17_B)  // standardized with a mean of 0 and a standard deviation of 1


* Hopefulness: 
gen hopeful = q17_C
replace hopeful = hopeful/10
lab def hopeful 0 "V. little hope" 1 "V. high hope"
lab val hopeful hopeful

** Quasi pre-treatment outcome variables: 
* Fairness
gen pretreat_fair = q1
replace pretreat_fair=. if q1==6
replace pretreat_fair = (pretreat_fair - 1)/4
lab val pretreat_fair fairness
lab var pretreat_fair "Pretreatment fairness"

* Effort
gen pretreat_effort = q2
replace pretreat_effort=. if q2==6
replace pretreat_effort = (pretreat_effort-1)/4
label def effort_pre 0 "Only chance" 1 "Only effort" 
label val pretreat_effort effort_pre

lab var pretreat_effort "Pretreatment effort"

** Covariates (pre-treatment)
* Race centrality scale
revrs q3_1,replace		
revrs q3_6,replace
factor q3_1 q3_2 q3_3 q3_4 q3_5 q3_6,pcf  //generating two RC-scales because q3_1 & q3_6 reflects another dimension than the others
alpha q3_2 q3_3 q3_4 q3_5 , casewise gen(RC_1) min(1)
alpha q3_1 q3_6, casewise gen(RC_2) min(1)



* Social dominance orientation (higher values = higher SDO)
revrs q5_3,replace
revrs q5_4,replace
revrs q5_7,replace
revrs q5_8,replace

factor q5_1 q5_2 q5_3 q5_4 q5_5 q5_6 q5_7 q5_8,pcf

alpha q5_1 q5_2 q5_5 q5_6 , casewise gen(SDO_1) min(1)
alpha q5_3 q5_4 q5_7 q5_8 , casewise gen(SDO_2) min(1)

* Compassion
revrs q6_2,replace
revrs q6_3,replace
revrs q6_5,replace
factor q6_1 q6_2 q6_3 q6_4 q6_5,pcf
alpha q6_1 q6_4 , casewise gen(COM_1) min(1)
alpha q6_2 q6_3 q6_5, casewise gen(COM_2) min(1)

* Ideology
gen lib_con = q7
replace lib_con =. if q7 ==8 | q7==9

* Party ID
gen party=q8
replace party=. if q8==6 | q8==7

**household income
tab household_income_za

gen hh_income_old= household_income_za
replace household_income_za = . if hh_income_old>19
rename household_income_za hh_income


*Country
gen country=2

save "data/ZA_data_clean.dta", replace


**# Cleaning the US data
use "data/US_survey.dta",clear 

** Attenion screeners: 
gen attention_1 = 1 
replace attention_1 = 0 if qScreen1 == 1

gen attention_2 = .
replace attention_2 = 0 if qScreen2_3 == 1 & qScreen2_5 == 1  		// respondents who selected both green AND red (as instructed)
replace attention_2 = 1 if qScreen2_1 != 0 | qScreen2_2 != 0 | 		///
	qScreen2_4 != 0 | qScreen2_6 != 0 | qScreen2_7 != 0 | 			///
	qScreen2_8 !=0 | qScreen2_9 != 0 

gen screenouts = 1 
replace screenouts = 0 if attention_1 == 0 & attention_2 == 0 		// Final sample = 3.049 respondents who passed both attention screeners. 	

drop if screenouts == 1					// dropping screenouts

***Factual manipulation check
gen factual_mani=0
replace factual_mani =1 if q14_E1==2 | q14_E2==1
replace factual_mani=. if BlockE==5 | BlockE==3 | BlockE==4

** Treatment variable: 
recode BlockE (5=0 "Control group") ///
	(1=2 "Intergroup ineq") ///
	(2=1 "Interpersonal ineq") ///
	(3=3 "Within black ineq") ///
	(4=4 "Within white ineq") ///
	, gen(D)

** Outcome variables: 
* Perceived fairness: 
gen fair = q15 
replace fair=. if q15==8
sum fair,de
replace fair = ((fair -`r(min)')/(`r(max)'-`r(min)'))*1				// Doesn't work - did the oldschool way above...
lab def fairness 0 "Very unfair" 1 "Very fair" 
lab val fair fairness

egen fair_std = std(q15) if q15!=8   // standardized with a mean of 0 and a standard deviation of 1

* Perceived effort: 
gen effort = q16
replace effort=. if q16==77
sum effort,de
replace effort = ((effort -`r(min)')/(`r(max)'-`r(min)'))*1		// same
lab def effort 0 "Only luck" 1 "Only effort" 
lab val effort effort

egen effort_std = std(q16) if q16!=77   // standardized with a mean of 0 and a standard deviation of 1


* Grievances (anger): 
gen angry = q17_A
replace angry = angry/10
lab def angry 0 "V. low anger" 1 "V. high anger"
lab val angry angry

egen angry_std = std(q17_A)   // standardized with a mean of 0 and a standard deviation of 1


* Grievances (frustration): 
gen frustrated = q17_B
replace frustrated = frustrated/10
lab def frustrated 0 "V. low frustration" 1 "V. high frustration"
lab val frustrated frustrated

egen frustrated_std = std(q17_B)   // standardized with a mean of 0 and a standard deviation of 1


* Hopefulness: 
gen hopeful = q17_C
replace hopeful = hopeful/10
lab def hopeful 0 "V. little hope" 1 "V. high hope"
lab val hopeful hopeful

** Quasi pre-treatment outcome variables: 
* Fairness
gen pretreat_fair = q1
replace pretreat_fair=. if q1==6
replace pretreat_fair = (pretreat_fair - 1)/4
lab val pretreat_fair fairness
lab var pretreat_fair "Pretreatment fairness"

* Effort
gen pretreat_effort = q2
replace pretreat_effort=. if q2==6
replace pretreat_effort = (pretreat_effort-1)/4
label def effort_pre 0 "Only chance" 1 "Only effort" 
label val pretreat_effort effort_pre
lab var pretreat_effort "Pretreatment effort"


** Covariates (pre-treatment)
* Race centrality scale
revrs q3_1,replace	
revrs q3_6,replace
factor q3_1 q3_2 q3_3 q3_4 q3_5 q3_6,pcf  //generating two RC-scales because q3_1 & q3_6 reflects another dimension than the others
alpha q3_2 q3_3 q3_4 q3_5, casewise gen(RC_1) min(1) 
alpha q3_1 q3_6, casewise gen(RC_2) min(1)



* Social dominance orientation (higher values = higher SDO)
revrs q5_3,replace
revrs q5_4,replace
revrs q5_7,replace
revrs q5_8,replace

factor q5_1 q5_2 q5_3 q5_4 q5_5 q5_6 q5_7 q5_8,pcf

alpha q5_1 q5_2 q5_5 q5_6 , casewise gen(SDO_1) min(1)
alpha q5_3 q5_4 q5_7 q5_8 , casewise gen(SDO_2) min(1)

* Compassion
revrs q6_2,replace
revrs q6_3,replace
revrs q6_5,replace
factor q6_1 q6_2 q6_3 q6_4 q6_5,pcf
alpha q6_1 q6_4 , casewise gen(COM_1) min(1)
alpha q6_2 q6_3 q6_5, casewise gen(COM_2) min(1)

* Ideology
gen lib_con = q7
replace lib_con =. if q7 ==8 | q7==9

* Party ID
gen party=q8
replace party=. if q8==6 | q8==7

**household income
tab profile_gross_household

gen hh_income_old= profile_gross_household
replace profile_gross_household = . if hh_income_old>16
replace profile_gross_household = 13 if profile_gross_household>=13 & profile_gross_household!=.
rename profile_gross_household hh_income


*Country
gen country=3

save "data/US_data_clean.dta", replace

